package com.ktjiaotu.action;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import org.apache.log4j.Logger;

import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * @Author: DABINGCHNEGJIAO
 * @CreateTime: 2025-06-05
 * @Description: Cglib动态代理工厂
 * @Version: 1.0
 */
public class CglibDynamicProxyFatctory {
    private static Logger logger = Logger.getLogger(CglibDynamicProxyFatctory.class);
    private static MethodInterceptor interceptor = new MethodInterceptor() {
        @Override
        public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
            logger.debug(Arrays.toString(args) + "编写准备->打开了电脑");
            logger.debug(Arrays.toString(args) + "编写准备->打开了软件");
            Object result = methodProxy.invokeSuper(o, args);  //调用父类的原始的方法
            logger.debug(Arrays.toString(args) + "编写完成->关闭了软件");
            logger.debug(Arrays.toString(args) + "编写完成->关闭了电脑");
            return result;
        }
    };

    public static <T> T create(Class<T> clazz) {
        Enhancer enhancer = new Enhancer();
        enhancer.setCallback(interceptor);
        enhancer.setSuperclass(clazz);
        return (T) enhancer.create();
    }
}
